package com.dmwork.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.service.base.exception.DmworkException;
import com.dmwork.vod.service.VodService;
import com.dmwork.vod.util.AliyunVodUtils;
import com.dmwork.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.List;

@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodProperties vodProperties;

    @Override
    public String uploadVideo(InputStream inputStream, String originalFilename) {
        String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));

        UploadStreamRequest request = new UploadStreamRequest(vodProperties.getKeyid(), vodProperties.getKeysecret(), title, originalFilename, inputStream);
        request.setTemplateGroupId(vodProperties.getTemplateGroupId());
        request.setWorkflowId(vodProperties.getWorkflowId());
        request.setCateId(vodProperties.getCateId());
        log.info(vodProperties.getCateId().toString());
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        String videoId = response.getVideoId();
        if (StringUtils.isEmpty(videoId)) {
            log.error("阿里云上传失败：" + response.getCode() + " - " + response.getMessage());
            throw new DmworkException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        } else {
            return videoId;
        }
    }

    @Override
    public void removeVideo(String videoId) throws ClientException {
        DefaultAcsClient client = AliyunVodUtils.initVodClient(vodProperties.getRegionId(), vodProperties.getKeyid(), vodProperties.getKeysecret());
        DeleteVideoRequest request = new DeleteVideoRequest();
        //支持传入多个视频ID，多个用逗号分隔
        request.setVideoIds(videoId);
        client.getAcsResponse(request);
    }

    @Override
    public void removeVideoByVideoIdList(List<String> videoIdList) throws ClientException {
        DefaultAcsClient client = AliyunVodUtils.initVodClient(vodProperties.getRegionId(), vodProperties.getKeyid(), vodProperties.getKeysecret());
        DeleteVideoRequest request = new DeleteVideoRequest();
        //每次执行20条记录的批量删除
        StringBuilder videoIds = new StringBuilder();
        int size = videoIdList.size();
        for (int i = 0; i < size; i++) {
            videoIds.append(videoIdList.get(i));
            if (i==size-1 || i % 20 == 19) { //如果遍历完毕或者已添加了20条videoId，则执行一次批量删除
                log.info("i->"+i);
                request.setVideoIds(videoIds.toString());
                client.getAcsResponse(request);
                videoIds=new StringBuilder();
            } else { //否则在videoId后添加一个逗号
                videoIds.append(",");
            }
        }
    }

    @Override
    public String getPlayAuth(String videoSourceId) throws ClientException {
        //初始化client对象
        DefaultAcsClient client = AliyunVodUtils.initVodClient(vodProperties.getRegionId(), vodProperties.getKeyid(), vodProperties.getKeysecret());

        //创建请求对象
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoSourceId);

        //获取响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        //获取播放凭证
        return response.getPlayAuth();
    }
}
